/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package csse3004.GD;

import java.util.ArrayList;

/**
 *
 * @author Tom McNair - S41758984
 */
public class SRQuestion extends Question {

    /**
     * only set when the questionType is Short response
     * 0 for "Numeric", 1 for "Alphanumeric", -1 for not set
     */
    private int answerType = -1;
    private ArrayList<SRAnswer> answers = new ArrayList<SRAnswer>();

    public SRQuestion() {
        super.setSR();
    }

    public SRQuestion(ArrayList<SRAnswer> answers) {
        super.setSR();
        this.answers=answers;
    }

    public SRQuestion(int id, String text, boolean numeric) {
        super.setSR();
        super.id=id;
        this.setText(text);
        answerType=(numeric)?0:1;
    }
    
    
    /**
     * sets the question type to numeric
     */
    public void setNumeric() {
        this.answerType = 0;
    }

    /**
     * sets the question type to alphanumeric
     */
    public void setAlphanumeric() {
        this.answerType = 1;
    }

    /**
     * returns 0 if the type is Numeric, 1 if the type is Alphanumeric
     *  @return 0 means Numeric, 1 means Alphanumeric, -1 means not set
     */
    public int getAnswerType() {
        return answerType;
    }

    public boolean isNumeric() {
        if (answerType == 0) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isAlphanumeric() {
        if (answerType == 1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Automatically generate the HTML for displaying either type of question.
     * Primarily designed for the viewing poll pages
     * @return the HTML that can be shoved directly into the window
     */
    public String displayQuestion(int user) {
        //this was made because David hates the stupid JSTL and so wanted to do 
        //all the logic here  
        String html = "";
        if (user == 1){            
            html+="<p>please enter a ";
            html += (this.getAnswerType() == 0) ? "numeric" : "alpanumeric";
            html += " answer</p>";
            html+="<input type=\"textarea\"/>";
        }if (user == 0){
            html = "<p>Please enter a ";
            html += (this.getAnswerType() == 0) ? "numeric" : "alpanumeric";
            html += " answer</p>";
        }        
            return html;
    }

    public ArrayList<SRAnswer> getAnswers() {
        return answers;
    }

    /**
     * Sets the answers related to this question
     * requires id to be set
     * @param Answer List of answers to set (and overwrite the existing set answers)
     * @return true if success, false if ID isn't set (unsuccessful)
     */
    /**
     * Sets the answers related to this question
     * requires id to be set
     * @param Answer List of answers to set (and overwrite the existing set answers)
     * @return true if success, false if ID isn't set (unsuccessful)
     */
    public boolean setAnswers(ArrayList<SRAnswer> Answers) {
        if (id == null) {
            return false;
        }
        for (SRAnswer a : Answers) {
            a.setId(id);
        }
        this.answers = Answers;
        return true;
    }

    public boolean addAnswer(SRAnswer Answer) {
        if (id == null) {
            return false;
        }
        Answer.setId(id);
        this.answers.add(Answer);
        return true;
    }
}
